/*
 * 文 件 名:  EncodingFilter.java
 * 版    权:  Huawei Technologies Co., Ltd. Copyright 2008-9999,  All rights reserved
 * 描    述:  <描述>请求过滤器类
 * 创 建 人:  kouwenbing
 * 创建时间:  2008-8-16
 * 修 改 人:  kouwenbing
 * 修改时间:  2008-8-16
 * 跟踪单号:  <跟踪单号>
 * 修改单号:  <修改单号>
 * 修改内容:  <修改内容>
 */
package com.chinastar.iweb.common.filter;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.chinastar.iweb.user.domain.UserBean;

/**
 * 请求过滤器
 * @author  JGring
 * @version  [版本号, 2008-8-18]
 * @see  [相关类/方法]
 * @since  [产品/模块版本]
 */
public class EncodingFilter extends HttpServlet implements Filter
{
    
    private static final long serialVersionUID = 1L;
    
    private static final String CONTENT_TYPE = "text/html;charset=UTF-8"; 
    
    private String targetEncoding = "UTF-8";
    
    /**
     *  初始化
     * @param config  FilterConfig 暂时不用
     * @throws ServletException  Servlet异常
     */
    public void init(FilterConfig config) throws ServletException
    {
        this.targetEncoding = config.getInitParameter("encoding");
    }
    
    /** 执行过滤
     * @param request 请求
     * @param response 返回
     * @param chain chain
     * @throws IOException IO异常
     * @throws ServletException Servlet异常
     */
    public void doFilter(ServletRequest request, ServletResponse response,
            FilterChain chain) throws IOException, ServletException
    {
        //session过滤
        HttpServletRequest httpServletRequest = (HttpServletRequest) request;
        HttpServletResponse httpServletResponse = (HttpServletResponse) response;
        
        int contextIndex = httpServletRequest.getRequestURL().toString().lastIndexOf("/");
        String path = httpServletRequest.getRequestURL().toString().substring(contextIndex + 1, httpServletRequest.getRequestURL().toString().length());
        if (!(path.equals("login.action") || path.equals("") || path.equals("index.jsp")))
        {
            HttpSession session = httpServletRequest.getSession();
            if (null != session)
            {
                UserBean userBean = (UserBean) session.getAttribute("userBean");
                if (valid(userBean))
                {
                    httpServletResponse.sendRedirect(httpServletRequest.getContextPath() + "/index.jsp");
                }
            }
            else 
            {
                httpServletResponse.sendRedirect(httpServletRequest.getContextPath() + "/index.jsp");
            }
        }
        
        //字符过滤
        request.setCharacterEncoding(targetEncoding);
        response.setContentType(CONTENT_TYPE);
        if (chain != null)
        {
            chain.doFilter(request, response);
        }
    }
    
    private boolean valid(UserBean userBean)
    {
        if (null != userBean)
        {
            if (userBean.getUserNum().trim().length() > 0)
            {
                return false;
            }
        }
        return true;
    }
    
}
